package com.woniu.controller.advisor;

import com.woniu.entity.response.ResponseEntity;
import com.woniu.exception.controllerException.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.One;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestControllerAdvice(basePackages = "com.woniu.controller")
@Slf4j
public class ControllerExceptionAdvisor {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity validHandler(MethodArgumentNotValidException ex) {
        BindingResult result = ex.getBindingResult();
        if (result.hasFieldErrors()) {
            Map<String, String> map = new HashMap<>(10);
            List<FieldError> errors = result.getFieldErrors();
            errors.forEach(e -> map.put(e.getField(), e.getDefaultMessage()));
            return ResponseEntity.fail(map);
        }
        return ResponseEntity.fail(null);
    }

//    @ExceptionHandler({JWTException.class})
//    public ResponseEntity JWTHandler(JWTException j) {
//        return ResponseEntity.fail(null, "用户未登录",ResponseEntity.TOKEN_NOT_FOUND);
//    }

    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity simpleField(ConstraintViolationException c) {
        Map<String, String> map = new HashMap<>();
        c.getConstraintViolations().forEach(e -> map.put(e.getPropertyPath().toString(), e.getMessage()));
        return ResponseEntity.fail(map);
    }

    /**
     * 捕获controller 账户不存在的异常
     * @param b
     * @return
     */
    @ExceptionHandler(BankAccountNotFoundException.class)
    public ResponseEntity BankAccountIsExist(BankAccountNotFoundException b) {
        Map<String, String> map = new HashMap<>();
        b.printStackTrace();
        return ResponseEntity.fail("fail","账户为空或已注销或不存在",ResponseEntity.BANK_ACCOUNT_NOT_FOUND);
    }

    @ExceptionHandler(MoneyOutOfRangeException.class)
    public ResponseEntity BankAccountIsExist(MoneyOutOfRangeException m) {
        m.printStackTrace();
        return ResponseEntity.fail("fail","银行账户余额不足，扣款失败",ResponseEntity.BANK_MONEY_NOT_ENOUGH);
    }

    @ExceptionHandler(BankFileException.class)
    public ResponseEntity BankFileError(BankFileException bf) {
        bf.printStackTrace();
        return ResponseEntity.fail("fail","文件格式不是excel格式",ResponseEntity.BANK_MONEY_NOT_ENOUGH);
    }

    @ExceptionHandler(CellBeingNullException.class)
    public ResponseEntity CellIsNull(CellBeingNullException cbn) {
        cbn.printStackTrace();
        return ResponseEntity.fail("fail","单元格内容不能为空、错误或者为空字符串",ResponseEntity.BANK_MONEY_NOT_ENOUGH);
    }

    @ExceptionHandler(CellTypeErrorException.class)
    public ResponseEntity CellTypeError(CellTypeErrorException ct) {
        ct.printStackTrace();
        return ResponseEntity.fail("fail","单元格内容格式错误",ResponseEntity.BANK_MONEY_NOT_ENOUGH);
    }


    @ExceptionHandler(Exception.class)
    public ResponseEntity handler(Exception e) {
        e.printStackTrace();
        return ResponseEntity.fail("fail", e.getMessage());
    }
}
